//
//  CartoonItemListViewController.swift
//  UUCartoon
//
//  Created by Galaxy on 2024/2/2.
//  Copyright © 2024 qykj. All rights reserved.
//

import UIKit
import UICollectionViewLeftAlignedLayout
class CartoonItemListViewController: BaseViewController {
    
    private var pageNum = 1
    private var listArr:[CartoonModel] = []
    public var websiteModel:WebsiteBaseModel = WebsiteBaseModel.init()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setNavColor(navColor: UIColor(.dm, light: .white, dark: .black), titleColor: UIColor(.dm, light: .black, dark: .white), barStyle: .default)
        let rightItem = UIBarButtonItem.init(title: "跳页", style: .plain, target: self, action: #selector(jumpPage))
        navigationItem.rightBarButtonItem = rightItem
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        getListData()
    }
    @objc func jumpPage(){
        let alert = UIAlertController.init(title: "跳页", message: "请输入跳转页码", preferredStyle: .alert)
        let sureAction = UIAlertAction.init(title: "确定", style: .default) { action in
            self.listArr = []
            let textField = alert.textFields?.first
            self.pageNum = Int((textField?.text!)!)!
            self.getListData()
        }
        alert.addAction(sureAction)
        alert.addTextField { textField in
            textField.placeholder = "请输入页码"
            textField.text = "\(self.pageNum-1)"
            textField.keyboardType = .numberPad
        }
        let cancelAction = UIAlertAction.init(title: "取消", style: .cancel)
        alert.addAction(cancelAction)
        present(alert, animated: true)
    }
    // 获取列表
    func getListData(){
        beginProgress()
        DispatchQueue.global().async { [self] in
            // 最新发布
            DataTool.init().getNewCartoonData(type: websiteModel.type, pageNum: pageNum) { resultArr in
                DispatchQueue.main.async { [self] in
                    endProgress()
                    mainTable.es.stopLoadingMore()
                    if !resultArr.isEmpty {
                        pageNum += 1
                    }
                    listArr.append(array: resultArr)
                    mainTable.listArr = listArr
                }
            }
        }
    }
    // 漫画列表
    lazy var mainTable: UUTableView = {
        let tableView = UUTableView.init(frame: .zero, style: .plain)
        view.addSubview(tableView)
        tableView.estimatedRowHeight = 150
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        tableView.register(UINib.init(nibName: "ComicTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "cell")
        tableView.cellForRow {[weak self] (indexPath) in
            let model = self!.listArr[indexPath.row]
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ComicTableViewCell
            cell.model = model
            cell.rankViewConstraintWidth.constant = 40
            return cell
        }
        tableView.cellItemDidSelected {[weak self] (indexPath) in
            let model = self!.listArr[indexPath.row]
            let VC = CartoonDetailViewController.init()
            VC.model = model.chapterArr[0].data[0]
            VC.cartoonModel = model
            VC.chapter_area = 0
            VC.type = self!.websiteModel.type
            self?.navigationController?.pushViewController(VC, animated: true)
        }
        tableView.es.addInfiniteScrolling(animator: footer) {[weak self] in
            self!.getListData()
        }
        return tableView
    }()
}
